package models

import "time"

type HebeiPact struct {
	Id             int       `xorm:"not null pk autoincr INT(11)"`
	SchoolId       int       `xorm:"INT(11)"`
	CharstandardId int       `xorm:"INT(11)"`
	CarType        string    `xorm:"VARCHAR(255)"`
	ClassName      string    `xorm:"VARCHAR(255)"`
	Content        string    `xorm:"TEXT"`
	Addtime        time.Time `xorm:"DATETIME"`
	Updatetime     time.Time `xorm:"DATETIME"`
}

// 获取合同模板列表
func (c *HebeiPact) GetList(school_id string, pagesize, offset int) (count int64, list []*HebeiPact, err error) {
	err = engine.Table("dts_hebei_pact").
		Where("school_id = ?", school_id).
		Limit(pagesize, offset).
		Desc("id").
		Find(&list)
	count, _ = engine.Table("dts_hebei_pact").
		Where("school_id = ?", school_id).
		Count()
	return
}

// 查询是否存在相同合同模板
func (c *HebeiPact) GetByCond(school_id, car_type, charstandard_id string) (has bool, list HebeiPact, err error) {
	has, err = engine.Table("dts_hebei_pact").
		Where("school_id = ? and charstandard_id = ? and car_type = ?", school_id, charstandard_id, car_type).
		Desc("id").
		Get(&list)
	return
}

// 添加合同模板
func (c *HebeiPact) Add() bool {
	if _, err := engine.Insert(c); err != nil {
		return false
	}
	return true
}

// 修改合同模板
func (c *HebeiPact) Edit(fields ...string) bool {
	var (
		err error
	)
	_, err = engine.ID(c.Id).Cols(fields...).Update(c)
	if err != nil {
		return false
	}
	return true
}

// GetHeBeiPact 获取合同模板数据
func (m *HebeiPact) GetHeBeiPact(schoolID, charStandID int, carType string) (bool, error) {
	return engine.Table("dts_hebei_pact").Where("school_id=? and car_type=? and charstandard_id=?", schoolID, carType, charStandID).Get(m)
}
